﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace eproject_BusTicketOnline
{
    public partial class CreateTicket : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
                con.Open();
                SqlDataAdapter daBUS = new SqlDataAdapter("SELECT BuslistId FROM Buslist", con);
                DataSet dsBus = new DataSet();
                daBUS.Fill(dsBus);
                ddlBusId.DataSource = dsBus;
                ddlBusId.DataTextField = "BuslistId";
                ddlBusId.DataBind();
                // SqlDataAdapter daPayer = new SqlDataAdapter("SELECT     dbo.Member.FullName FROM         dbo.Member INNER JOIN                      dbo.BusticKetDetail ON dbo.Member.MemberId = dbo.BusticKetDetail.MemberId", con);
                SqlDataAdapter daPayer = new SqlDataAdapter("SELECT Member.FullName FROM Member ", con);
                DataSet dsPayer = new DataSet();
                daPayer.Fill(dsPayer);
                ddlFullname.DataSource = dsPayer;
                ddlFullname.DataTextField = "Fullname";
                ddlFullname.DataBind();
                con.Close();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                string busid = ddlBusId.SelectedValue;
                string payer = ddlFullname.SelectedValue;
                string passenger = txtFullName.Text;
                string age = ddlAge.SelectedValue;
                Double price = Double.Parse(lblPrice.Text);
                // int price = Convert.ToInt32(lblPrice.Text);
                Double lastPrice = 0;
                if (age == "Less 5 year old")
                {
                    lastPrice = price * 0;
                }
                else
                    if (age == "From 5 to 12")
                    {
                        lastPrice = price / 2;
                    }
                    else
                        if (age == "From 13 to 50")
                        {
                            lastPrice = price;
                        }
                        else
                            if (age == "Above 50")
                            {
                                lastPrice = price - (price * 30 / 100);
                            }
                SqlConnection con1 = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
                con1.Open();
                // get memberid with Fullname 
                SqlCommand cmdFullName = new SqlCommand("SELECT Member.MemberID FROM Member where Member.FullName='" + payer + "'", con1);
                SqlDataReader drFullName;
                drFullName = cmdFullName.ExecuteReader();
                while (drFullName.Read())
                {
                    lblMemberId.Text = drFullName["MemberId"].ToString();
                }
                con1.Close();
                //

                SqlConnection con5 = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
                con5.Open();
                SqlCommand cmdadd = new SqlCommand("INSERT INTO BusticketDetail(MemberId,BusListId,FullName,Age,DeparturePlace,ArrivePlace,StartDate,EndDate,Time,Price,TicketStatus) VALUES ('"
                                                                                + lblMemberId.Text
                                                                                + "','" + busid + "','" + txtFullName.Text
                                                                                + "','" + ddlAge.SelectedValue + "','" + lblDeparturePlace.Text
                                                                                + "','" + lblArrivePlace.Text + "','" + lblStartDay.Text
                                                                                + "','" + lblEndDay.Text + "','" + lblTime.Text
                                                                                + "'," + (lastPrice) + ",'active')", con5);
                cmdadd.ExecuteNonQuery();
                con5.Close();
                Response.Redirect("TicketManage.aspx");
            }
            catch
            {
                Label1.Visible = true;
            }
        }

        protected void ddlBusId_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label1.Visible = false;
            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
            string busid = ddlBusId.SelectedValue;
            con.Open();
            SqlCommand cmdDPlace = new SqlCommand("SELECT DeparturePlace From Buslist where BusListId='" + busid + "'", con);
            SqlDataReader drDPlace;
            drDPlace = cmdDPlace.ExecuteReader();
            while (drDPlace.Read())
            {
                lblDeparturePlace.Text = drDPlace["DeparturePlace"].ToString();
            }
            con.Close();

            con.Open();
            SqlCommand cmdAPlace = new SqlCommand("SELECT ArrivePlace From Buslist where BusListId='" + busid + "'", con);
            SqlDataReader drAPlace;
            drAPlace = cmdAPlace.ExecuteReader();
            while (drAPlace.Read())
            {
                lblArrivePlace.Text = drAPlace["ArrivePlace"].ToString();
            }
            con.Close();

            con.Open();
            SqlCommand cmdStartDay = new SqlCommand("SELECT CONVERT(varchar(20),[StartDate],101) as StartDate From Buslist where BusListId='" + busid + "'", con);
            SqlDataReader drStartDay;
            drStartDay = cmdStartDay.ExecuteReader();
            while (drStartDay.Read())
            {
                lblStartDay.Text = drStartDay["StartDate"].ToString();
            }
            con.Close();

            con.Open();
            SqlCommand cmdEndDay = new SqlCommand("SELECT CONVERT(varchar(20),[EndDate],101) as EndDate From Buslist where BusListId='" + busid + "'", con);
            SqlDataReader drEndDay;
            drEndDay = cmdEndDay.ExecuteReader();
            while (drEndDay.Read())
            {
                lblEndDay.Text = drEndDay["EndDate"].ToString();
            }
            con.Close();

            con.Open();
            SqlCommand cmdTime = new SqlCommand("SELECT Time From Buslist where BusListId='" + busid + "'", con);
            SqlDataReader drTime;
            drTime = cmdTime.ExecuteReader();
            while (drTime.Read())
            {
                lblTime.Text = drTime["Time"].ToString();
            }
            con.Close();

            con.Open();
            SqlCommand cmdPrice = new SqlCommand("SELECT Price From Buslist where BusListId='" + busid + "'", con);
            SqlDataReader drPrice;
            drPrice = cmdPrice.ExecuteReader();
            while (drPrice.Read())
            {
                lblPrice.Text = drPrice["Price"].ToString();
            }
            con.Close();
            Page.DataBind();
        }

        protected void btnchoose_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
        }

        protected void btnChoose_Click(object sender, EventArgs e)
        {
            string seat = "";
            if (seat1.Checked)
            {
                seat = seat1.Text;
            }
            else if (seat1.Checked)
            {
                seat = seat1.Text;
            }
            else if (seat2.Checked)
            {
                seat = seat2.Text;
            }
            else if (seat3.Checked)
            {
                seat = seat3.Text;
            }
            else if (seat4.Checked)
            {
                seat = seat4.Text;
            }
            else if (seat5.Checked)
            {
                seat = seat5.Text;
            }
            else if (seat6.Checked)
            {
                seat = seat6.Text;
            }
            else if (seat7.Checked)
            {
                seat = seat7.Text;
            }
            else if (seat8.Checked)
            {
                seat = seat8.Text;
            }
            else if (seat9.Checked)
            {
                seat = seat9.Text;
            }
            else if (seat10.Checked)
            {
                seat = seat10.Text;
            }
            else if (seat11.Checked)
            {
                seat = seat11.Text;
            }
            else if (seat12.Checked)
            {
                seat = seat12.Text;
            }
            else if (seat13.Checked)
            {
                seat = seat13.Text;
            }
            else if (seat14.Checked)
            {
                seat = seat14.Text;
            }
            else if (seat15.Checked)
            {
                seat = seat15.Text;
            }
            else if (seat16.Checked)
            {
                seat = seat16.Text;
            }
            else if (seat17.Checked)
            {
                seat = seat17.Text;
            }
            else if (seat18.Checked)
            {
                seat = seat18.Text;
            }
            else if (seat19.Checked)
            {
                seat = seat19.Text;
            }
            else if (seat20.Checked)
            {
                seat = seat20.Text;
            }

            lblSeatId.Text = seat;
            Panel1.Visible = false;
            btnchoose.Text = "Change seat";
        }
    }
}